<!DOCTYPE html>
<html ng-app="MyAPP">
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <link href="http://cdn.bootcss.com/jquery-timepicker/1.8.9/jquery.timepicker.min.css" rel="stylesheet">

    <style>
        .demo{
            margin-bottom:10px;
        }
    </style>
</head>
<body>

 <div class="container">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <div class="row demo">
         <h3>Demo1: timepicker示例</h3>
         <div ng-controller="TimePickerCtrl">
             <input type='text' class="time ui-timepicker-input" ng-model="timeOfDay" time-picker/>
             <button ng-click="setTime()">设置时间</button>
         </div>
     </div>
 </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script src="http://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/jquery-timepicker/1.8.9/jquery.timepicker.min.js"></script>
<script>
    var app=angular.module("MyAPP",[]);
    app.controller('TimePickerCtrl',function($scope){
        $scope.setTime = function(){
            //强制设置到2:58pm这个时间点
            $scope.timeOfDay = 53907764
        }
    });
    app.directive('timePicker',function(){
        return {
            restrict:'EA',
            require:'^?ngModel',
            link:function(scope,element,attrs,ctrl){

                ctrl = ctrl || {"$setViewValue":angular.noop}; //angular.noop代表一个空函数

                ctrl.$setViewValue(0);

                $(element).timepicker().on('changeTime',function(){
                    var seconds = $(element).timepicker('getSecondsFromMidnight');
                    //设置数据模型: 就是从午夜0点到所选择的时间的毫秒值
                    ctrl.$setViewValue(seconds * 1000);
                    console.log(scope.timeOfDay);
                });

                ctrl.$render = function(){
                    //根据1970 年 1 月 1 日到新的时间点的毫秒数 设置时间
                    //$viewValue就是timeOfDay,是从午夜0点到选择时间的毫秒数
                    console.log(ctrl.$viewValue);
                    $(element).timepicker('setTime', new Date(getTodayZero().getTime() + ctrl.$viewValue));
                };

            }
        }
    });

    function getTodayZero(){
        var today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        today.setMilliseconds(0);
        return today;
    }
</script>
